package com.controllers.forms;



import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * Created by Administrator on 2017/5/9.
 */
@SuppressWarnings("deprecation")
public class PaginatorForm {
    private int offset;
    private int limit;
    private Long total = 0L;
    private String queryStringKey;
    private Integer queryIntegerKey;


    private List<FilterCondition> filterConditions = new ArrayList<>();

    public int getOffset() {
        return offset;
    }

    public void setOffset(int offset) {
        this.offset = offset;
    }

    public int getLimit() {
        return limit;
    }

    public void setLimit(int limit) {
        this.limit = limit;
    }

    public Long getTotal() {
        return total;
    }

    public void setTotal(Long total) {
        this.total = total;
    }


    public String getQueryStringKey() {
        return queryStringKey;
    }

    public void setQueryStringKey(String queryStringKey) {
        this.queryStringKey = queryStringKey;
    }

    public Integer getQueryIntegerKey() {
        return queryIntegerKey;
    }

    public void setQueryIntegerKey(Integer queryIntegerKey) {
        this.queryIntegerKey = queryIntegerKey;
    }

    public static class FilterCondition {
        private String left;
        private Object right;
        private String op; // op支持eq, gt, ge, lt, le, ne, like,eager(leftJoin)
        private int leftType;//1 boolean ,2 Integer
        private boolean common = true;

        public String getLeft() {
            return left;
        }

        public void setLeft(String left) {
            this.left = left;
        }

        public Object getRight() {
            return right;
        }

        public void setRight(Object right) {
            this.right = right;
        }

        public String getOp() {
            return op;
        }

        public void setOp(String op) {
            this.op = op;
        }


        public int getLeftType() {
            return leftType;
        }

        public void setLeftType(int leftType) {
            this.leftType = leftType;
        }

        public boolean isCommon() {
            return common;
        }

        public void setCommon(boolean common) {
            this.common = common;
        }
    }

    public List<FilterCondition> getFilterConditions() {
        return filterConditions;
    }

    public void setFilterConditions(List<FilterCondition> filterConditions) {
        this.filterConditions = filterConditions;
    }


//    public void addRestrictions(Criteria c, Class aClass) {
//        HashMap<String, Criteria> nestingCriteria = new HashMap<>();
//
//        for (FilterCondition filterCondition : getCommonFilterConditions()) {
//            if (filterCondition.getLeftType() == 1) {
//                filterCondition.setRight(filterCondition.getRight().equals("true") ? true : false);
//            } else if (filterCondition.getLeftType() == 2) {
//                filterCondition.setRight(Integer.parseInt((String) filterCondition.getRight()));
//            } else if (filterCondition.getLeftType() == 3) {
//                Field fields[] = aClass.getDeclaredFields();
//                for (Field field : fields) {
//                    if (field.getName().equals(filterCondition.getLeft())) {
//                        filterCondition.setRight(field.getType().getEnumConstants()[Integer.parseInt((String) filterCondition.getRight())]);
//                        break;
//                    }
//                }
//            }
//
//
//            String[] leftSplit = filterCondition.getLeft().split("\\.");
//            Criteria criteria = c;
//            String queryColumn = filterCondition.getLeft();
//
//            if (leftSplit.length > 1) {
//                String mapStr = filterCondition.getLeft().substring(0, filterCondition.getLeft().lastIndexOf("."));
//
//                if (nestingCriteria.containsKey(mapStr)) {
//                    criteria = nestingCriteria.get(mapStr);
//                    queryColumn = leftSplit[leftSplit.length - 1];
//                } else {
//                    for (int i = 0; i < leftSplit.length - 1; i++) {
//                        criteria = criteria.createCriteria(leftSplit[i]);
//                        queryColumn = leftSplit[i + 1];
//                    }
//                    nestingCriteria.put(mapStr, criteria);
//                }
//            }
//
//            if (filterCondition.op.equals("like")) {
//                criteria.add(Restrictions.like(queryColumn, "%" + filterCondition.getRight() + "%"));
//            } else if (filterCondition.op.equals("eq")) {
//                criteria.add(Restrictions.eq(queryColumn, filterCondition.getRight()));
//            } else if (filterCondition.op.equals("order")) {
//                if (((String) filterCondition.getRight()).equals("asc")) {
//                    criteria.addOrder(Order.asc(queryColumn));
//                } else if (((String) filterCondition.getRight()).equals("desc")) {
//                    criteria.addOrder(Order.desc(queryColumn));
//                }
//            }
//        }
//    }


    public List<FilterCondition> getCommonFilterConditions() {
        List<FilterCondition> result = new ArrayList<>();
        for (FilterCondition filterCondition : getFilterConditions()) {
            if (filterCondition.common) {
                result.add(filterCondition);
            }
        }
        return result;
    }


    public List<FilterCondition> getNotCommonFilterConditions() {
        List<FilterCondition> result = new ArrayList<>();
        for (FilterCondition filterCondition : getFilterConditions()) {
            if (!filterCondition.common) {
                result.add(filterCondition);
            }
        }
        return result;
    }


    public void addLogicDeleteFilterCondition(Boolean result) {
        FilterCondition filterCondition = new FilterCondition();
        filterCondition.setLeft("logicDelete");
        filterCondition.setOp("eq");
        filterCondition.setRight(result);
        filterCondition.setLeftType(1);
        this.getFilterConditions().add(filterCondition);
    }
}
